package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.BookMlAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.BookDetailInfoSubBean;
import com.shuorikeji.huohuo.bean.BookZjInfoBean;
import com.shuorikeji.huohuo.bean.BookZjMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.GsonUtil;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookMlActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_book_tips)
    TextView tvBookTips;
    @BindView(R.id.iv_zj_sort)
    ImageView ivZjSort;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private BookDetailInfoSubBean bookInfoDta;
    private int BOOK_ID;
    private int curPge;
    private int pageSize;
    private List<BookZjInfoBean> mlList = new ArrayList<>();
    private BookMlAdapter bookMlAdapter;
    private int PL_COUNT;
    private int totalZjCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_ml);
    }

    @Override
    protected void initData() {
        curPge = 1;
        pageSize = 20;
        initSmartRefreshLayout();
        initAdapter();
        initIntent();
        initClick();
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(BookMlActivity.this, LinearLayoutManager.VERTICAL, false));
        bookMlAdapter = new BookMlAdapter(BookMlActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookZjInfoBean data = (BookZjInfoBean) object;
                        //Intent intent = new Intent(BookMlActivity.this, ReadBookActivity.class);
                        Intent intent = new Intent(BookMlActivity.this, ReadBookTempActivity.class);
                        intent.putExtra("BOOK_ID", BOOK_ID);
                        intent.putExtra("PL_COUNT", PL_COUNT);
                        intent.putExtra("ZJ_ID", data.getId());
                        intent.putExtra("TOTAL_ZJ_COUNT", totalZjCount);
                        startActivity(intent);
                        break;
                }
            }
        });
        rvData.setAdapter(bookMlAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        String bookInfoStr = intent.getStringExtra("BOOK_INFO");
        BOOK_ID = intent.getIntExtra("BOOK_ID", -1);
        bookInfoDta = GsonUtil.parseJsonToBean(bookInfoStr, BookDetailInfoSubBean.class);
        PL_COUNT = intent.getIntExtra("PL_COUNT", 0);
        getBookZj();
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(BookMlActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPge = 1;
                getBookZj();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPge = curPge + 1;
                getBookZj();
            }
        });
    }

    private void getBookZj() {
        mPresenter.getBookZj(BookMlActivity.this, curPge, pageSize, BOOK_ID, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        srlLayout.finishRefresh();
                        srlLayout.finishLoadMore();
                        BookZjMainBean data = (BookZjMainBean) object;
                        totalZjCount = data.getData().getChapterList().getTotal();
                        if (bookInfoDta != null) {
                            tvTitle.setText(bookInfoDta.getName());
                            tvBookTips.setText(((bookInfoDta.getPublish_status() == 1) ? "连载中" : "已完结") +
                                    " " + data.getData().getChapterList().getTotal() + " 章节");
                        }
                        if (curPge == 1) {
                            mlList.clear();
                        }
                        mlList.addAll(data.getData().getChapterList().getData());
                        bookMlAdapter.setData(mlList);
                        break;
                    case 2:
                        srlLayout.finishRefresh();
                        srlLayout.finishLoadMore();
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookMlActivity.this, msg);
                        break;
                }
            }
        });
    }
}